package com.rickpan.service;

import com.rickpan.entity.TeamInviteCode;
import com.rickpan.entity.TeamMember;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Optional;

/**
 * 团队邀请码服务接口
 */
public interface TeamInviteCodeService {

    /**
     * 生成团队邀请码
     */
    TeamInviteCode generateInviteCode(Long teamId, Long creatorId, TeamMember.Role inviteRole);

    /**
     * 生成团队邀请码（带过期时间）
     */
    TeamInviteCode generateInviteCode(Long teamId, Long creatorId, TeamMember.Role inviteRole, 
                                     LocalDateTime expireTime);

    /**
     * 生成团队邀请码（带使用次数限制）
     */
    TeamInviteCode generateInviteCode(Long teamId, Long creatorId, TeamMember.Role inviteRole, 
                                     Integer maxUses);

    /**
     * 生成团队邀请码（完整参数）
     */
    TeamInviteCode generateInviteCode(Long teamId, Long creatorId, TeamMember.Role inviteRole, 
                                     LocalDateTime expireTime, Integer maxUses);

    /**
     * 通过邀请码加入团队
     */
    TeamMember joinTeamByInviteCode(String inviteCode, Long userId);

    /**
     * 根据邀请码查找
     */
    Optional<TeamInviteCode> findByInviteCode(String inviteCode);

    /**
     * 验证邀请码是否有效
     */
    boolean isValidInviteCode(String inviteCode);

    /**
     * 获取团队的所有邀请码
     */
    List<TeamInviteCode> getTeamInviteCodes(Long teamId);

    /**
     * 获取团队的活跃邀请码
     */
    List<TeamInviteCode> getActiveInviteCodes(Long teamId);

    /**
     * 分页获取团队邀请码
     */
    Page<TeamInviteCode> getTeamInviteCodes(Long teamId, Pageable pageable);

    /**
     * 禁用邀请码
     */
    void disableInviteCode(Long inviteCodeId, Long operatorId);

    /**
     * 禁用邀请码（通过邀请码字符串）
     */
    void disableInviteCode(String inviteCode, Long operatorId);

    /**
     * 删除邀请码
     */
    void deleteInviteCode(Long inviteCodeId, Long operatorId);

    /**
     * 更新邀请码
     */
    TeamInviteCode updateInviteCode(TeamInviteCode inviteCode);

    /**
     * 刷新邀请码（重新生成）
     */
    TeamInviteCode refreshInviteCode(Long inviteCodeId, Long operatorId);

    /**
     * 获取邀请码使用统计
     */
    InviteCodeStats getInviteCodeStats(Long teamId);

    /**
     * 清理过期的邀请码
     */
    int cleanupExpiredCodes();

    /**
     * 清理用完的邀请码
     */
    int cleanupExhaustedCodes();

    /**
     * 获取用户创建的邀请码
     */
    List<TeamInviteCode> getUserInviteCodes(Long userId);

    /**
     * 检查用户是否可以创建邀请码
     */
    boolean canCreateInviteCode(Long userId, Long teamId);

    /**
     * 获取即将过期的邀请码
     */
    List<TeamInviteCode> getExpiringSoonCodes(Long teamId);

    /**
     * 邀请码统计信息
     */
    class InviteCodeStats {
        private long totalCodes;
        private long activeCodes;
        private long expiredCodes;
        private long exhaustedCodes;
        private long disabledCodes;
        private long totalUses;

        // 构造函数
        public InviteCodeStats() {}

        public InviteCodeStats(long totalCodes, long activeCodes, long expiredCodes, 
                              long exhaustedCodes, long disabledCodes, long totalUses) {
            this.totalCodes = totalCodes;
            this.activeCodes = activeCodes;
            this.expiredCodes = expiredCodes;
            this.exhaustedCodes = exhaustedCodes;
            this.disabledCodes = disabledCodes;
            this.totalUses = totalUses;
        }

        // Getter和Setter方法
        public long getTotalCodes() {
            return totalCodes;
        }

        public void setTotalCodes(long totalCodes) {
            this.totalCodes = totalCodes;
        }

        public long getActiveCodes() {
            return activeCodes;
        }

        public void setActiveCodes(long activeCodes) {
            this.activeCodes = activeCodes;
        }

        public long getExpiredCodes() {
            return expiredCodes;
        }

        public void setExpiredCodes(long expiredCodes) {
            this.expiredCodes = expiredCodes;
        }

        public long getExhaustedCodes() {
            return exhaustedCodes;
        }

        public void setExhaustedCodes(long exhaustedCodes) {
            this.exhaustedCodes = exhaustedCodes;
        }

        public long getDisabledCodes() {
            return disabledCodes;
        }

        public void setDisabledCodes(long disabledCodes) {
            this.disabledCodes = disabledCodes;
        }

        public long getTotalUses() {
            return totalUses;
        }

        public void setTotalUses(long totalUses) {
            this.totalUses = totalUses;
        }

        @Override
        public String toString() {
            return "InviteCodeStats{" +
                    "totalCodes=" + totalCodes +
                    ", activeCodes=" + activeCodes +
                    ", expiredCodes=" + expiredCodes +
                    ", exhaustedCodes=" + exhaustedCodes +
                    ", disabledCodes=" + disabledCodes +
                    ", totalUses=" + totalUses +
                    '}';
        }
    }
}
